<?php defined('BASEPATH') OR exit('No direct script access allowed');

// Code here is run before admin controllers
class Admin_Controller extends MY_Controller {

	public function Admin_Controller()
	{
		parent::__construct();
                $admin_theme = Settings::get('admin_theme1');                  
                $this->load->library('template',array(
                        'theme_admin_id' => 1
                    )
                );                
                //$this->_check_access();
                //echo $admin_theme;die;
	}

	private function _check_access()
	{
		// These pages get past permission checks
		$ignored_pages = array('admin/login', 'admin/logout');

		// Check if the current page is to be ignored
		$current_page = $this->uri->segment(1, '') . '/' . $this->uri->segment(2, 'index');

		// Dont need to log in, this is an open page
		if (in_array($current_page, $ignored_pages))
		{
			return TRUE;
		}
		else if ( ! $this->user)
		{
			redirect('admin/login');
		}

		// Admins can go straight in
		else if ($this->user->group === 'admin')
		{
			return TRUE;
		}

		// Well they at least better have permissions!
		else if ($this->user)
		{
			// We are looking at the index page. Show it if they have ANY admin access at all
			if ($current_page == 'admin/index' && $this->permissions)
			{
				return TRUE;
			}
			else
			{
				// Check if the current user can view that page
				return array_key_exists($this->module, $this->permissions);
			}
		}

		// god knows what this is... erm...
		return FALSE;
	}

}